package utile;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.*;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.logging.Logger;

    public class HttpRequest {
        Logger log = Logger.getLogger(String.valueOf(jmeter.HttpRequest.class));
        public HttpResponse Request(String url, Object object) {
            HeaderUtil headerUtil=new HeaderUtil();
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost(url);
            HttpResponse httpResponse = null;
            try {
                StringEntity stringEntity = new StringEntity(object.toString());
//            log.info(">>>>>>>>>>>>>>>"+object.toString());
                httpPost.setEntity(stringEntity);
                httpPost.setHeaders(headerUtil.GetHeader());
                httpResponse = httpClient.execute(httpPost);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return httpResponse;
        }
        //对写的post请求进行测试,可以查看返回结果，我这边写的是查看返回StatusCode
        public static void main(String[] args) throws IOException {
            jmeter.HttpRequest httpRequest=new jmeter.HttpRequest();
            String url="http://xxxx:xxx/login";
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("userName","xxx");
            jsonObject.put("password","xxxx");
            jsonObject.put("url","xxxx");
            HttpResponse httpResponse=httpRequest.Request(url,jsonObject);
            String entity= EntityUtils.toString(httpResponse.getEntity());
            System.out.println(">>>>>>>>>>"+httpResponse.getStatusLine().getStatusCode());
        }
    }
